package com.cnswhy.cloud.server.comsumer.consumer.cps;

import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.common.entity.enumeration.SplitType;
import com.cnswhy.cloud.server.common.entity.mq.RegisterMsg;
import com.cnswhy.cloud.server.common.entity.mq.UpdateCustomerSplitMQ;
import com.cnswhy.cloud.server.comsumer.event.RegisterMessageEvent;
import com.cnswhy.cloud.server.comsumer.event.split.UpdateCustomerSplitEvent;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerSplit;
import com.cnswhy.cloud.server.core.service.CustomerSplitService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 给用户添加CPS分账比例配置
 */
@Component
@Slf4j
public class CreateCustomerSplitConsumer implements RegisterMessageEvent, UpdateCustomerSplitEvent {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerSplitService customerSplitService;
    @Override
    public void register(RegisterMsg registerMsg) {
        log.info("给用户添加CPS分账比例配置:{} {}",registerMsg.getCustomerId(),registerMsg.getLoginName());
        Customer customer = customerService.getById(registerMsg.getCustomerId());
        Customer promoter = customerService.getById(registerMsg.getPromoterId());
        if(promoter != null){
            CustomerSplit customerSplit = new CustomerSplit();
            customerSplit.setCustomerId(promoter.getId());
            customerSplit.setSplitRate(0L);
            customerSplit.setCurrRate(BigDecimal.ZERO);
            customerSplit.setUpRate(BigDecimal.ZERO);
            customerSplit.setDownCustomerId(customer.getId());
            customerSplit.setDownCustomerPhone(customer.getLoginName());
            customerSplit.setStatus("0");
            customerSplit.setSplitType(SplitType.NO_CARD);
            customerSplitService.save(customerSplit);
        }else{
            CustomerSplit customerSplit = new CustomerSplit();
            customerSplit.setCustomerId(0L);
            customerSplit.setSplitRate(100L);
            customerSplit.setCurrRate(BigDecimal.valueOf(100));
            customerSplit.setUpRate(BigDecimal.ZERO);
            customerSplit.setDownCustomerId(customer.getId());
            customerSplit.setDownCustomerPhone(customer.getLoginName());
            customerSplit.setStatus("1");
            customerSplit.setSplitType(SplitType.NO_CARD);
            customerSplitService.save(customerSplit);
        }
        log.info("给用户添加CPS分账比例配置：完成");

    }

    @Override
    public void update(UpdateCustomerSplitMQ updateCustomerSplitMQ) {
        log.info("下级费率更新:{} {}",updateCustomerSplitMQ.getCustomerId());
        //TODO
        CustomerSplit customerSplit = customerSplitService.getOne(Wraps.<CustomerSplit>lbQ().eq(CustomerSplit::getDownCustomerId, updateCustomerSplitMQ.getCustomerId()).eq(CustomerSplit::getSplitType, updateCustomerSplitMQ.getSplitType()));
        List<CustomerSplit> customerSplitList = customerSplitService.list(Wraps.<CustomerSplit>lbQ().eq(CustomerSplit::getCustomerId, updateCustomerSplitMQ.getCustomerId())
            .eq(CustomerSplit::getSplitType, updateCustomerSplitMQ.getSplitType()));
        for(CustomerSplit split : customerSplitList){
            updateDown(customerSplit,split);
        }
        /*Customer customer = customerService.getById(updateCustomerSplitMQ.getCustomerId());
        while (customer != null){
            customer =
        }*/

    }
    public void updateDown(CustomerSplit customerSplit,CustomerSplit downCustomerSplit){
        downCustomerSplit.setCurrRate(customerSplit.getCurrRate().multiply(BigDecimal.valueOf(downCustomerSplit.getSplitRate().doubleValue() / 100)));
        downCustomerSplit.setUpRate(customerSplit.getCurrRate().subtract( downCustomerSplit.getCurrRate()));
        customerSplitService.updateById(downCustomerSplit);
        List<CustomerSplit> customerSplitList = customerSplitService.list(Wraps.<CustomerSplit>lbQ().eq(CustomerSplit::getCustomerId, downCustomerSplit.getDownCustomerId())
                .eq(CustomerSplit::getSplitType, downCustomerSplit.getSplitType()));
        for(CustomerSplit split : customerSplitList){
            updateDown(downCustomerSplit, split);
        }
    }
}
